// pages/mine/mine.js
var app = getApp()
var util = require('../../utils/util.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    signInfo: "积分:0\n今日未签到"
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    wx.getStorage({
      key: app.globalData.storage_userinfo_key,
      success: function(res) {
        console.log(res);
        that.setData({
          userInfo: res.data,
        })
        if (res.data.userIsSign == true) {
          that.setData({
            signInfo: "积分:" + res.data.userIntegral + "\n今日已签到"
          })
        } else {
          that.setData({
            signInfo: "积分:" + res.data.userIntegral + "\n今日未签到"
          })
        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.authDialog = this.selectComponent("#authDialog");
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 用户头像点击
   */
  onUserHeadClick: function() {
    if (app.globalData.userInfo != null) {
      console.log("查看大图")
    } else {
      this.authDialog.showDialog();
    }
  },
  /**
   * 用户姓名点击
   */
  onUserNameClick: function() {
    if (app.globalData.userInfo != null) {} else {
      this.authDialog.showDialog();
    }
  },
  /**
   * 签到点击
   */
  onSignClick: function() {
    var that = this;
    if (app.globalData.userInfo != null) {
      util.netWorkUtil('user/sign', {
          userNo: app.globalData.userInfo.userNo,
          userIntegral: app.globalData.userInfo.userIntegral,
          userSignTime: app.globalData.userInfo.userSignTime
        },
        function(err, res) {
          if (res.data.data != null) {
            app.globalData.userInfo = res.data.data
          }
          console.info(res);
          if (res.data.code == 200) {
            that.setData({
              signInfo: "积分:" + res.data.data.userIntegral + "\n今日已签到"
            });
          }
          wx.showModal({
            title: '温馨提示',
            content: res.data.message,
            showCancel: false, //不显示取消按钮
            confirmText: '知道了'
          })
        });
    } else {
      this.authDialog.showDialog();
    }
  },
  /**
   * vip点击
   */
  onVipClick: function() {
    if (app.globalData.userInfo != null) {
      console.log("去vip")
    } else {
      this.authDialog.showDialog();
    }
  },
  /**
   * 上传影片点击
   */
  onUpLoadClick: function() {
    if (app.globalData.userInfo != null) {
      console.log("去上传电影")
    } else {
      this.authDialog.showDialog();
    }
  },
  /**
   * 我的收藏点击
   */
  onMyFarvoiteClick: function() {
    if (app.globalData.userInfo != null) {
      console.log("去我的收藏");
    } else {
      this.authDialog.showDialog();
    }
  },
  /**
   * 下载管理点击
   */
  onDownLoadClick: function() {
    if (app.globalData.userInfo != null) {
      console.log("去下载管理");
    } else {
      this.authDialog.showDialog();
    }
  },
  /**
   * 设置点击
   */
  onSettingClick: function() {
    if (app.globalData.userInfo != null) {
      console.log("去设置");
    } else {
      this.authDialog.showDialog();
    }
  },
  cancelEvent: function() {
    this.authDialog.hideDialog();
  },
  confirmEvent: function() {
    this.authDialog.hideDialog();
  },

  bindGetUserInfo: function(e) {
    var that = this
    // 用户点击授权后，这里可以做一些登陆操作
    if (e.detail.detail.userInfo != null) {
      getApp().globalData.userInfo = e.detail.detail.userInfo;
      wx.getUserInfo({
        success(res) {
          console.log(res);
        }
      })
      wx.setStorage({
        key: app.globalData.storage_userinfo_key,
        data: e.detail.detail.userInfo,
      })
      that.setData({
        userInfo: getApp().globalData.userInfo
      })
    } else {
      wx.showModal({
        title: '登录失败',
        content: '请重新登录，以享受更多功能',
        showCancel: false, //不显示取消按钮
        confirmText: '确定'
      })
    }
  },

})